package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.TianJieMember;
import org.itboys.mobile.service.mongo.mobile.TianJieMobileMemberService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_金融_会员_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/member")
public class TianJieMobileMemberController extends BaseController{
    @Autowired
    private TianJieMobileMemberService memberService;
    @Autowired
    private ResourceHolder resourceHolder;

    @RequestMapping(value="/checkIsCaptain",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【会员-市场管理】校验用户是否是团队队长（返回:1是0否）；打开个人中心时调用；控制前端显示【市场管理】菜单")
    public void checkIsCaptain(HttpServletRequest request,
                           HttpServletResponse response,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                           @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            Integer isCaptain =memberService.checkIsCaptain(memberId);
            ResponseMessage.success(isCaptain);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/member/checkIsCaptain----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/checkIsCaptain2",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【会员-市场管理】2017年12月13日。新街口会返回需要的数据，校验用户是否是团队队长（返回:1是0否）；打开个人中心时调用；控制前端显示【市场管理】菜单")
    public void checkIsCaptain2(HttpServletRequest request,
                               HttpServletResponse response,
                               @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                               @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            Map<String,Object> result =memberService.checkIsCaptain2(memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/member/checkIsCaptain2----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/updateBitAddress",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【会员】修改用户小比特币地址（提币账号）")
    public void updateBitAddress(HttpServletRequest request,
                                HttpServletResponse response,
                                 @ApiParam(value = "bitAddress")@RequestParam(value = "bitAddress", required = true) String bitAddress,
                                @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                                @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
           //2017年8月28日
       //     memberService.updateBitAddress(memberId,bitAddress);
        //    ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/member/updateBitAddress----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/getGroupInfo",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【会员-市场管理】团队管理员获取团队中所有信息。含：团队名称，团队队长，团队人数合计，团队人数")
    public void getGroupInfo(HttpServletRequest request,
                          HttpServletResponse response,
                          @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                          @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            Map<String,Object> result=memberService.getGroupInfo(memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/member/getGroupInfo----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    @RequestMapping(value="/getAchievement",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【会员-业绩管理】获取我的团队成员及成员业绩")
    public void getAchievement(HttpServletRequest request,
                             HttpServletResponse response,
                             @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                             @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
                             @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空") @RequestParam(value = "sort", required = false) String sort,
                             @ApiParam(required = false, name = "order", value = "排序参数。。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                             @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                             @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            //2017年8月28日
            List<Map<String,Object>> result=memberService.getAchievement(request,memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/member/getAchievement----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 分享主页
     * @param model
     * @return
     */
    @RequestMapping(value="/share",method= RequestMethod.GET)
    @ApiOperation(value="【会员】我的分享页。" +
            "分享文本：万品天街邀请您的加入," +
            "分享标题：万品天街邀请您的加入," +
            "分享图片：app的图标，" +
            "分享链接：服务器+/tianjie/mobile/finance/member/share/reg?memberId=用户ID")
    public String index( @ApiParam(value = "推荐人ID")@RequestParam(value = "memberId", required = true) Long memberId,
                        Model model){
        TianJieMember member = memberService.getById(memberId);
        //2017年11月10日使用联图网直接在线挂载二维码
        String shareUrl =resourceHolder.getStringValue("mobileRoot")+"/tianjie/mobile/finance/member/share/reg?memberId="+memberId;
       member.setFinanceEwm("http://qr.liantu.com/api.php?bg=FFFFFF&fg=ff0000&gc=222222&el=l&w=200&m=10&text="+shareUrl);
       member.setLogo(resourceHolder.getStringValue("imgRoot")+member.getLogo());

        model.addAttribute("memberDto",member);

        return "/tianjie/member/share/index";
    }

    /**
     * 分享注册页
     * @param request
     * @param model
     * @return
     */

    @RequestMapping(value="/share/reg",method= RequestMethod.GET)
    @ApiOperation(value="【会员-分享-注册页】分享注册页。实际分享的是此地址。不需要app打开")
    public String share_reg(HttpServletRequest request,
                        @ApiParam(value = "推荐人ID")@RequestParam(value = "memberId", required = true) Long memberId,
                        Model model){
        TianJieMember member = memberService.getById(memberId);

        member.setLogo(resourceHolder.getStringValue("imgRoot")+member.getLogo());
        model.addAttribute("memberDto",member);
        return "/tianjie/member/share/reg";
    }
    @RequestMapping(value="/share/reg/submit",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【会员-推广】提交注册信息")
    public void share_reg_submit(HttpServletRequest request,
                             HttpServletResponse response,
                         @ApiParam(value = "随机数")@RequestParam(value = "memberId", required = true) Long memberId,
                         @ApiParam(value = "验证码")@RequestParam(value = "validateCode", required = true) String validateCode,
                         @ApiParam(value = "手机号码")@RequestParam(value = "mobile", required = true) String mobile,
                         @ApiParam(value = "密码")@RequestParam(value = "password", required = true) String password

    ) {
        try{
      //   memberService.share_reg( memberId, validateCode, mobile, password);
        //    ResponseMessage.success();
            ResponseMessage.error(-1,"此业务已暂停，请联系万品天街");
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/finance/mobile/member/share/reg/submit----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 分享主页
     * @param request
     * @param model
     * @return
     */

    @RequestMapping(value="/share/reg/done",method= RequestMethod.GET)
    @ApiOperation(value="【会员-分享-注册页】分享注册页。。不需要app打开")
    public String index(HttpServletRequest request,
                        @ApiParam(value = "推荐人ID")@RequestParam(value = "memberId", required = true) Long memberId,
                        Model model){
        TianJieMember member = memberService.getById(memberId);
        member.setLogo(resourceHolder.getStringValue("imgRoot")+member.getLogo());
        model.addAttribute("memberDto",member);
        return "/tianjie/member/share/done";
    }
}
